package com.along.textdemo;

import android.Manifest;
import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

import com.along.textdemo.base.BaseActivity;
import com.along.textdemo.databinding.ActivityVoicePrintBinding;
import com.along.textdemo.manager.AudioManager;
import com.along.textdemo.model.VoicePrintViewModel;
import com.along.textdemo.utils.FileUtils;


/**
 * 声纹：获取声纹注册
 */
public class VoicePrintActivity extends BaseActivity<VoicePrintViewModel, ActivityVoicePrintBinding>implements AudioManager.AudioStateListener {

    private static String TAG = "VoicePrintActivity";

    private static final int MSG_AUDIO_PREPARED = 0x110;
    private static final int MSG_VOICE_CHANGE = 0x111;
    private static final int MSG_VOICE_DISMISS = 0x112;
    private AudioManager mAudioManager;//音频的管理类
    private boolean isRecording = false;//是否正在录音
    private float mTime;//计时
    private String audioPath = "";

    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {
        @SuppressLint("HandlerLeak")
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case MSG_AUDIO_PREPARED:
                    isRecording = true;
                    new Thread(mGetVoiceLevelRunnable).start();
                    Log.e(TAG,"获取完毕开启线程="+isRecording);
                    break;
                case MSG_VOICE_CHANGE:

                    int bd = mAudioManager.getVoiceLevel(100);
                    Log.e(TAG,"获取音量="+bd);
                    binding.voicePrintWave.putValue(bd);
                    break;
                case MSG_VOICE_DISMISS:
                    binding.voicePrintWave.stopAnim();
                    break;

            }
        }
    };

    /**
     * 获取音量大小，并计时
     */
    private Runnable mGetVoiceLevelRunnable = new Runnable() {
        @Override
        public void run() {
            while (isRecording) {
                SystemClock.sleep(50);
                mTime += 0.1f;
                mHandler.sendEmptyMessage(MSG_VOICE_CHANGE);
                Log.e(TAG,"音频开始有变化=");
            }
        }
    };

    @Override
    protected int getContentViewId() {
        return R.layout.activity_voice_print;
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected void processLogic() {
        //初始化音频
        initAudio();
        //长按录音按钮
        binding.voicePrintBtn.setOnTouchListener(new View.OnTouchListener() {
            @SuppressLint("ClickableViewAccessibility")
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                //松开,录音结束
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    binding.voicePrintTv.setText(R.string.str_recorder_normal);
                    if (isRecording) {
                        //释放资源
                        mAudioManager.release();
                        //获取音频文件
                        audioPath = mAudioManager.getmCurrentFilePath();
                        Toast.makeText(VoicePrintActivity.this,"获取音频文件=="+audioPath,Toast.LENGTH_SHORT).show();
                    }
                    reset();
                }

                //按下,开始录音
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    binding.voicePrintWave.stopAnim();
                    //准备audio
                    mAudioManager.prepareAudio();
                    mHandler.sendEmptyMessage(MSG_AUDIO_PREPARED);
                    binding.voicePrintTv.setText(R.string.str_recorder_recording);
                }
                return false;
            }
        });


    }

    private void initAudio() {
        mAudioManager = AudioManager.getmInstance(FileUtils.audioSavePath(this));
    }

    /**
     * 重置标识
     */
    private void reset() {
        isRecording = false;
        mTime = 0;
    }

    @Override
    public void wellPrepared() {
        mHandler.sendEmptyMessage(MSG_AUDIO_PREPARED);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mAudioManager != null){
            mAudioManager.cancel();
            mAudioManager = null;
        }
        if (mGetVoiceLevelRunnable != null)
            mHandler.removeCallbacks(mGetVoiceLevelRunnable);
    }


}
